/*
 * Copyright (c) 2009, Takashi Ito
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the authors nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "tssp.h"

#ifndef _TSSP_DVB_DR_02_H_
#define _TSSP_DVB_DR_02_H_

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/**
 * The structure of selector bytes for object carousel transport.
 *
 * {
 *     remote_connection                 1
 *     reserved_future_use               7
 *     if (remote_connection == "1") {
 *         original_network_id          16
 *         transport_stream_id          16
 *         service_id                   16
 *     }
 *     component_tag                     8
 * }
 */
struct tssp_dr_OC_transport {
    uint8_t remote_connection;
    uint16_t original_network_id;
    uint16_t transport_stream_id;
    uint16_t service_id;
    uint8_t component_tag;
};

/**
 * The structure of selector bytes for interaction channel transport.
 *
 * for (i = 0; i < N; i++) {
 *     URL_base_length                                      8
 *     for (j = 0; j < M; j++) {
 *         URL_base_byte                                    8
 *     }
 *     URL_extension_count                                  8
 *     for (j = 0; j < URL_extension_count; j++) {
 *         URL_extension_length                             8
 *         for (k = 0; k < URL_extension_length; k++) {
 *             URL_extension_byte                           8
 *         }
 *     }
 * }
 */
struct tssp_dr_IC_transport {
    uint8_t *URL_base;
    uint8_t URL_extension_count;
    uint8_t **URL_extension;
};

/**
 * The structure of transport protocol descriptor.
 *
 * transport_protocol_descriptor() {
 *     descriptor_tag                8
 *     descriptor_length             8
 *     protocol_id                  16
 *     transport_protocol_label      8
 *     for (i = 0; i < N; i++) {
 *         selector_byte             8
 *     }
 * }
 */
struct tssp_dr_transport_protocol {
    uint16_t protocol_id;
    uint8_t label;

    union {
        struct tssp_dr_OC_transport oc;
        struct {
            uint8_t count;
            struct tssp_dr_IC_transport ic[126]; /* (255 - 3) / 2 */
            uint8_t *list[249]; /* (255 - 3 - 3) do not access */
            uint8_t data[252]; /* (255 - 3) do not access */
        } ic;
        struct {
            uint8_t length;
            uint8_t data[252]; /* 255 - 3 */
        } chunk;
    } selector_byte;
};

int tssp_dr_transport_protocol_parse(struct tssp_dr_transport_protocol *dest,
                                     const struct tssp_descriptor *src);

struct tssp_dr_transport_protocol *
tssp_dr_transport_protocol_create(const struct tssp_descriptor *desc);

#ifdef __cplusplus
};
#endif /* __cplusplus */

#endif /* _TSSP_DVB_DR_02_H_ */

